﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Infrastructure;
using MSIACCSL2.Web.CostCenterServices;
using MSIACCSL2.Web.WarehouseServices;
using System.IO;
using WcfService1.DTO;

namespace MSIACCSL2.Web.ReportPage
{
    public partial class ReportDO : System.Web.UI.Page
    {
        private DataAccess<Object> DA;
        private ReportDocument SalesInvoiceSummary;
        private ReportDocument SalesInvoiceDetail;
        private CostCenterServices.CostCenterServicesClient costCenterClient;
        private string Tipe;

        public ReportDO()
        {
            DA = new DataAccess<object>();
            SalesInvoiceSummary = new ReportDocument();
            SalesInvoiceDetail = new ReportDocument();
            costCenterClient = new CostCenterServicesClient();
        } 

        protected void Page_Load(object sender, EventArgs e)
        {
                if (! IsPostBack)
                    LoadCostCenter();

                if (rblType.SelectedValue == "Summary")
                {
                    LoadReportSummary(DateTime.Parse(txtTgl1.Text, new CultureInfo("ID-id")), DateTime.Parse(txtTgl2.Text, new CultureInfo("ID-id")), cboCostCenter.SelectedValue);
                }
                else if (rblType.SelectedValue == "Detail")
                {
                    LoadReportDetail(DateTime.Parse(txtTgl1.Text, new CultureInfo("ID-id")), DateTime.Parse(txtTgl2.Text, new CultureInfo("ID-id")), cboCostCenter.SelectedValue);
                }
        }


        void LoadCostCenter()
        {
            var costCenters = costCenterClient.GetAllCostCenter().ToList();
            costCenters.Insert(0,new CostCenterDTO());
            foreach (var costCenter in costCenters)
            {
                ListItem li = new ListItem(costCenter.Description,costCenter.CostCenterId);
                cboCostCenter.Items.Add(li);
            }
        }
      
    
        protected void Button1_Click(object sender, EventArgs e)
        {

        }

   

        public void LoadReportSummary(DateTime tglStart,DateTime tglEnd,string costCenter)
        {
            StringBuilder sql = new StringBuilder();
            using (StreamReader sr = File.OpenText(Server.MapPath("~/sql/ReportDOSummary.txt")))
            {
                String str = string.Empty;
                while ((str = sr.ReadLine()) != null)
                    sql.Append(str);
            }

            sql.AppendFormat(" where d.DoDate>='{0}' AND d.DoDate<='{1}'", tglStart.ToString("yyyy-MM-dd"),
                             tglEnd.ToString("yyyy-MM-dd"));
            if (!string.IsNullOrEmpty(costCenter))
                sql.AppendFormat(" AND d.CostCenterId='{0}'", costCenter);

            var Source = DA.GetDataListDataSet(sql.ToString());
            string reportPath = Server.MapPath("~/Report/LapDOSummary.rpt");
            SalesInvoiceSummary.Load(reportPath);
            SalesInvoiceSummary.SetDataSource(Source.Tables[0]);
            SetupParameter(tglStart, tglEnd, SalesInvoiceSummary);
            CrystalReportViewer1.ReportSource = SalesInvoiceSummary;
            CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
        }


        public void LoadReportDetail(DateTime tglStart, DateTime tglEnd, string costCenter)
        {
            StringBuilder sql = new StringBuilder();
            using (StreamReader sr = File.OpenText(Server.MapPath("~/sql/ReportDODetail.txt")))
            {
                String str = string.Empty;
                while ((str = sr.ReadLine()) != null)
                    sql.Append(str);
            }

            sql.AppendFormat(" where d.DODate>='{0}' AND d.DODate<='{1}'", tglStart.ToString("yyyy-MM-dd"),
                             tglEnd.ToString("yyyy-MM-dd"));
            if (!string.IsNullOrEmpty(costCenter))
                sql.AppendFormat(" AND d.CostCenterId='{0}'", costCenter);

            var Source = DA.GetDataListDataSet(sql.ToString());
            string reportPath = Server.MapPath("~/Report/ReportDODetail.rpt");
            SalesInvoiceDetail.Load(reportPath);
            SalesInvoiceDetail.SetDataSource(Source.Tables[0]);
            SetupParameter(tglStart,tglEnd,SalesInvoiceDetail);
            CrystalReportViewer1.ReportSource = SalesInvoiceDetail;
            CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
        }

        private void SetupParameter(DateTime tglStart,DateTime tglEnd,ReportDocument reportDocument)
        {
            var param = new ParameterDiscreteValue();
            param.Value = tglStart.ToString("dd-MM-yyyy");
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            reportDocument.DataDefinition.ParameterFields["tglawal"].ApplyCurrentValues(paramValue);
            var param2 = new ParameterDiscreteValue();
            param2.Value = tglEnd.ToString("dd-MM-yyyy");
            var paramValue2 = new ParameterValues();
            paramValue2.Add(param2);
            reportDocument.DataDefinition.ParameterFields["tglakhir"].ApplyCurrentValues(paramValue2);
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.Unload += Page_Unload;
        }

        void Page_Unload(object sender, EventArgs e)
        {
            SalesInvoiceSummary.Clone();
            SalesInvoiceSummary.Dispose();

            SalesInvoiceDetail.Clone();
            SalesInvoiceDetail.Dispose();
        }
    }
}